package com.hsy.sunflowerbao.login.ui

import android.support.v4.content.res.ResourcesCompat
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
import com.flux.ui.adapter.FActivityRecycleAdapter
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.ui.adapter.BaseHolder
import com.hsy.sunflowerbao.login.data.ShopsData
import kotlinx.android.synthetic.main.item_shop.view.*
import kotlinx.android.synthetic.main.separator_line_horizontal.view.*
import java.lang.ref.WeakReference

/**
 * 选择店铺适配器
 * Created on 2018/3/27.
 *
 * @author Yang
 */
class ShopsAdapter(activity: ShopsActivity, var items: List<ShopsData>, private val index: Int) : FActivityRecycleAdapter<ShopsActivity>(WeakReference(activity)) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = BaseHolder(getItemView(R.layout.item_shop, parent))

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder as BaseHolder
        holder.itemView.shopName.text = items[position].name
        if (index != position) {
            holder.itemView.shopName.setTextColor(ResourcesCompat.getColor(mActivity.get()!!.resources, R.color.text_minor, null))
            holder.itemView.separatorLine.setBackgroundColor(ResourcesCompat.getColor(mActivity.get()!!.resources, R.color.separator_line_color, null))
        } else {
            val color = ResourcesCompat.getColor(mActivity.get()!!.resources, R.color.colorPrimary, null)
            holder.itemView.shopName.setTextColor(color)
            holder.itemView.separatorLine.setBackgroundColor(color)
        }
        holder.itemView.setOnClickListener { _ ->
            mActivity.get()?.selectShop(items[position].sid)
        }
    }

    override fun getItemCount(): Int = items.size
}